package com.szyjjk.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szyjjk.constants.SzyjjkConstant;
import com.szyjjk.entity.RunFilePath;
import com.szyjjk.entity.RunHisData;
import com.szyjjk.entity.RunHisDataExample;
import com.szyjjk.entity.RunMapPic;
import com.szyjjk.entity.RunMapPicExample;
import com.szyjjk.entity.RunSiteData;
import com.szyjjk.entity.SysHardWareData;
import com.szyjjk.entity.SysHardWareDataExample;
import com.szyjjk.entity.SysWeekly;
import com.szyjjk.entity.SysWeeklyExample;
import com.szyjjk.entity.SysWeeklyPic;
import com.szyjjk.mapper.RunHisDataMapper;
import com.szyjjk.mapper.RunMapPicMapper;
import com.szyjjk.mapper.SysHardWareDataMapper;
import com.szyjjk.mapper.SysWeeklyMapper;
import com.szyjjk.mapper.SysWeeklyPicMapper;
import com.szyjjk.service.RunFilePathrService;
import com.szyjjk.service.RunSiteService;
import com.szyjjk.util.DateUtil;
import com.szyjjk.util.FileUtil;
import com.szyjjk.util.UUIDUtils;
import com.szyjjk.util.WordUtil;


@Controller
@RequestMapping("/weekly")
public class WeeklyController {

	@Value("${spareFileJpgPath}")
	private String spareFileJpgPath;

	@Autowired
	private RunSiteService runSiteService;

	@Autowired
	private RunFilePathrService runFilePathrService;

	@Autowired
	private RunHisDataMapper runHisDataMapper;

	@Autowired
	private SysWeeklyMapper sysWeeklyMapper;
	@Autowired
	private RunMapPicMapper runMapPicMapper;
	@Autowired
	private SysWeeklyPicMapper sysWeeklyPicMapper;
	@Autowired
	private SysHardWareDataMapper  sysHardWareDataMapper;
	@RequestMapping("/downLoad.do")
	public void downLoad(HttpServletResponse response) {
		//当前周只能生成一次周报 其他都是读取已经生成的数据
		try {
			int getweekly = DateUtil.getweekly(new Date());
			SysWeeklyExample eee = new SysWeeklyExample();
			eee.createCriteria().andQishuEqualTo(getweekly+"");
			List<SysWeekly> selectByExample3 = sysWeeklyMapper.selectByExample(eee);
			if (CollectionUtils.isNotEmpty(selectByExample3)) {
				SysWeekly sysWeekly = selectByExample3.get(0);
				FileUtil.downloadFiles(response, sysWeekly.getWeeklyPath(), sysWeekly.getWeeklyName());
				return;
			}

			String templatePath = spareFileJpgPath + "template\\";
			File f = new File(templatePath);
			if (!f.exists()) {
				f.mkdirs();
			}

			System.out.println("下载周报逻辑");

			// 查询周报所需数据

			SysWeekly sysWeekly = new SysWeekly();
			sysWeekly.setWeeklyPath(templatePath);

			// 文件名称

			/** 用于组装word页面需要的数据 */
			Map<String, Object> dataMap = new HashMap<String, Object>();

			/** 组装数据 */
			String title = "深圳湾水质监测周报";

			String fileOnlyName = DateUtil.dateToString3(new Date()) + ".doc"; // 文件唯一名称
			dataMap.put("title", title);
			// 第几期
			dataMap.put("qi", "" + getweekly);
			Map<String, String> weekDate = DateUtil.getWeekDate(new Date());
			// 2021-04-11   TODO
			//String mondayDate = weekDate.get("mondayDate");
			//String sundayDate = weekDate.get("sundayDate");
			String mondayDate = "2021-03-29";
			String sundayDate ="2021-04-06";

			String[] split = sundayDate.split("-");
			String[] split1 = mondayDate.split("-");

			dataMap.put("m", split[1]);
			dataMap.put("d", split[2]);
			dataMap.put("m0", split1[1]);
			dataMap.put("d0", split1[2]);
			// 选一个站点进行查询 eb27023c6d9911eab058309c2317ee3e 南山站
			String zhandian = "eb27023c6d9911eab058309c2317ee3e";
			List<RunSiteData> listShenZ = runSiteService.findWeekly(zhandian, mondayDate, sundayDate);
			if(CollectionUtils.isEmpty(listShenZ)) {
				
				List<SysWeekly> selectByExample3a = sysWeeklyMapper.selectByExample(null);
				if (CollectionUtils.isNotEmpty(selectByExample3a)) {
					SysWeekly sysWeekly1 = selectByExample3a.get(0);
					FileUtil.downloadFiles(response, sysWeekly1.getWeeklyPath(), sysWeekly1.getWeeklyName());
					return;
				}else {
					return;
				}
				
			}
			// 风速Xm/s，空气温度X℃，相对湿度X%，雨量Xmm，气压XhPa，太阳辐射XW/m2
			sysWeekly.setQuertSite(zhandian);
			sysWeekly.setQuertStartDate(mondayDate);
			sysWeekly.setQuertEndDate(sundayDate);
			sysWeekly.setCreateDate(new Date());
			sysWeekly.setId(UUIDUtils.getUUID());
			sysWeekly.setTitleName(title);
			sysWeekly.setQishu(getweekly + "");
			sysWeekly.setEndM(split[1]);
			sysWeekly.setEndD(split[2]);
			sysWeekly.setStartM(split1[1]);
			sysWeekly.setStartD(split1[2]);
			sysWeekly.setWeeklyName(fileOnlyName);

			getAvg(sysWeekly, listShenZ);

			
			// 气象自动监测
			dataMap.put("wnd", sysWeekly.getWnd());
			dataMap.put("temp", sysWeekly.getTemp());
			dataMap.put("hmd", sysWeekly.getHmd());
			dataMap.put("pcp", sysWeekly.getPcp());
			dataMap.put("qiya", sysWeekly.getQiya());
			

			// 水质自动监测 查出水质*test2_r_oneM.his的值

			//
			RunFilePath rfp = runFilePathrService.findByFileName("%oneM.his%");
			String id = rfp.getId();
			// Turbidity
			/**  水质监测结果
			 * 温度 盐度 酸碱度(无) 浊度(Turbidity无) 溶解氧 叶绿素 蓝绿藻(无) 总氮 总磷 氨氮
			 */
			String[] names = new String[] { "Temp_C", "Sal", "pH", "Turb_NTU", "DO_ppm", "Chl_ppb", "PE_uL","TN_ppm","TP_ppm","NH3_ppm","SolarRadiation" };
			String[] values = new String[names.length];
			
			DecimalFormat df = new DecimalFormat("0.00");

			for (int i = 0; i < names.length; i++) {
				double d = 0d;
				SysHardWareDataExample example = new SysHardWareDataExample();
				
				example.createCriteria().andDataNameEnEqualTo(names[i])
						.andDataTimeGreaterThanOrEqualTo(DateUtil.stringToDate2(mondayDate))
						.andDataTimeLessThanOrEqualTo(DateUtil.stringToDate2(sundayDate));
				List<SysHardWareData> selectByExample = sysHardWareDataMapper.selectByExample(example);
				
				for (SysHardWareData rrrr : selectByExample) {
					String dataValue = rrrr.getDataValue();
					if(StringUtils.isBlank(dataValue)||dataValue.contains("NAN")) {
						dataValue = "0";
					}
					d += Double.parseDouble(dataValue);
				}
				if(d!=0) {
					d = d / selectByExample.size();
				}
				String format = df.format(d);
				values[i] = format;
			}
			
			
		
		
			
			
			sysWeekly.setSuiw(values[0]);
			sysWeekly.setYandu(values[1]);
			sysWeekly.setSjdu(values[2]);
			sysWeekly.setHunz(values[3]);
			sysWeekly.setRongjy(values[4]);
			sysWeekly.setYels(values[5]);
			sysWeekly.setLanlz(values[6]);
			sysWeekly.setZongd(values[7]);
			sysWeekly.setZongl(values[8]);
			sysWeekly.setAnnot(values[9]);
			// 辐射暂无数据-下载的是图片  辐射取浮标数据
			sysWeekly.setFuse(values[10]);
			dataMap.put("fuse", sysWeekly.getFuse());
			
			
			dataMap.put("suiw", sysWeekly.getSuiw());
			dataMap.put("yandu", sysWeekly.getYandu());
			dataMap.put("sjdu", sysWeekly.getSjdu());
			dataMap.put("hunz", sysWeekly.getHunz());
			dataMap.put("rongjy", sysWeekly.getRongjy());
			dataMap.put("yels", sysWeekly.getYels());
			dataMap.put("lanlz", sysWeekly.getLanlz());
			dataMap.put("zongD", sysWeekly.getZongd());
			dataMap.put("zongl", sysWeekly.getZongl());
			dataMap.put("anD", sysWeekly.getAnnot());
			// 获取上周记录 判断标准 ？
			SysWeeklyExample example = new SysWeeklyExample();
			example.createCriteria().andQishuEqualTo((getweekly - 1 + ""))
					.andQuertStartDateEqualTo(sysWeekly.getQuertStartDate())
					.andQuertEndDateEqualTo(sysWeekly.getQuertEndDate());
			List<SysWeekly> selectByExample = sysWeeklyMapper.selectByExample(example);
			if (CollectionUtils.isEmpty(selectByExample)) {
				// 达到（部分达到，未达到）
				dataMap.put("shifDaDao", "部分达到");
				// 好于（劣与，相当于）
				dataMap.put("bijiao", "相当于");
			} else {
				/**
				 * 比较上周的值 不知道咋比较 取 总氮比较
				 * 
				 */
				// 上周的
				String zongd = selectByExample.get(0).getZongd();
				// 这周的
				String zongd2 = sysWeekly.getZongd();
				// 达到（部分达到，未达到）
				dataMap.put("shifDaDao", "部分达到");
				if (Double.parseDouble(zongd) > Double.parseDouble(zongd2)) {
					// 上周大于这周 变好
					// 好于（劣与，相当于）
					dataMap.put("bijiao", "好于");
				} else {
					dataMap.put("bijiao", "相当于");
				}

			}
			String fileName = rfp.getFileName();
			String mapName = fileName.replaceAll(".his", ".map");
			/**  水质监测结果
			 * 温度 盐度 酸碱度(无) 浊度(Turbidity无) 溶解氧 叶绿素 蓝绿藻(无) 总氮 总磷 氨氮
			 */
			String[] names1 = new String[] { "Temp", "Salinity" ,"OXY", "Chlfa", "TotN","TotP","NH4"};
			for (int i = 0; i < names1.length; i++) {
				RunMapPicExample example1 = new RunMapPicExample();
				example1.createCriteria().andMapNameEqualTo(mapName).andMapCodeEqualTo(names1[i])
						.andMapTimeGreaterThanOrEqualTo(DateUtil.stringToDate2(mondayDate))
						.andMapTimeLessThanOrEqualTo(DateUtil.stringToDate2(sundayDate));
				example1.setOrderByClause("map_time  limit 1");

				List<RunMapPic> selectByExample2 = runMapPicMapper.selectByExample(example1);
				if(CollectionUtils.isNotEmpty(selectByExample2)) {
					String mapPicPath = selectByExample2.get(0).getMapPicPath();
					String mapPicName = selectByExample2.get(0).getMapPicName();
					// System.err.println(mapPicPath+mapPicName);
					File ff =new File(mapPicPath + mapPicName);
					if(ff.exists()) {
						dataMap.put("tu" + (i + 1), getImageStr(mapPicPath + mapPicName));
					}else {
						dataMap.put("tu" + (i + 1), "无");
					}
					
					
					
					SysWeeklyPic record = new SysWeeklyPic();
					record.setId(UUIDUtils.getUUID());
					record.setWeekId(sysWeekly.getId());
					record.setPicName(mapPicName);
					record.setPicPath(mapPicPath);
					record.setPicInfo(names1[i]);
					record.setSortNum(i+1);
					sysWeeklyPicMapper.insertSelective(record);
				}else {
					dataMap.put("tu" + (i + 1), "wu");
				}
				
			}

			/** 生成word */
			WordUtil.createWord(dataMap, "tem7.ftl", sysWeekly.getWeeklyPath(), sysWeekly.getWeeklyName());
			sysWeeklyMapper.insertSelective(sysWeekly);

			/** 导出 */
			FileUtil.downloadFiles(response, sysWeekly.getWeeklyPath(), sysWeekly.getWeeklyName());
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	private String getImageStr(String imgFile) {
		// String imgFile = "C:/Users/KingWang/Desktop/QQ截图20170707203921.png";
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Base64.getEncoder().encodeToString(data);
	}

	private void getAvg(SysWeekly sysWeekly, List<RunSiteData> listShenZ) {
		List<String> fengsu = new ArrayList<>();
		List<String> wendu = new ArrayList<>();
		List<String> shidu = new ArrayList<>();
		List<String> yuliang = new ArrayList<>();
		List<String> qiya = new ArrayList<>();
		// List<String> fushe = new ArrayList<>();

		for (RunSiteData runSiteData : listShenZ) {

			if (runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE)) {
				// 一天的平均风速
				String avgWnd = getAvgWnd(runSiteData);
				fengsu.add(avgWnd);
			} else if (runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE3)) {
				// 一天的平均气温
				String avgTmp = getAvgqiya(runSiteData);
				wendu.add(avgTmp);
			} else if (runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE2)) {
				// 相对湿度
				String avgHmd = getAvgHmd(runSiteData);
				shidu.add(avgHmd);
			} else if (runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE1)) {
				// 降雨
				String avgPcp = getAvgPcp(runSiteData);
				yuliang.add(avgPcp);
			} else if (runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE4)) {
				String qiyaAvg = getAvgqiya(runSiteData);
				qiya.add(qiyaAvg);
			} /*
				 * else if(runSiteData.getDataType().equals(SzyjjkConstant.DATETYPE1)){ //大气辐射
				 * 暂无数据 }
				 */

		}

		sysWeekly.setWeeklyNum(fengsu.size());

		DecimalFormat df = new DecimalFormat("0.00");

		double d = 0d;
		for (String string : fengsu) {
			double parseDouble = Double.parseDouble(string);
			d += parseDouble;
		}
		if (d == 0) {
			sysWeekly.setWnd("0");
		} else {
			d = d / fengsu.size();
			sysWeekly.setWnd(df.format(d));
		}

		d = 0d;
		for (String string : wendu) {
			double parseDouble = Double.parseDouble(string);
			d += parseDouble;
		}
		if (d == 0) {
			sysWeekly.setTemp("0");
		} else {
			d = d / wendu.size();
			sysWeekly.setTemp(df.format(d));
		}

		d = 0d;
		for (String string : shidu) {
			double parseDouble = Double.parseDouble(string);
			d += parseDouble;
		}
		if (d == 0) {
			sysWeekly.setHmd("0");
		} else {
			d = d / shidu.size();
			d = d * 100;
			DecimalFormat df1 = new DecimalFormat("0.00");
			sysWeekly.setHmd(df1.format(d));
		}

		d = 0d;
		for (String string : yuliang) {
			double parseDouble = Double.parseDouble(string);
			d += parseDouble;
		}
		if (d == 0) {
			sysWeekly.setPcp("0");
		} else {
			d = d / yuliang.size();
			sysWeekly.setPcp(df.format(d));
		}

		d = 0d;
		for (String string : qiya) {
			double parseDouble = Double.parseDouble(string);
			d += parseDouble;
		}
		if (d == 0) {
			sysWeekly.setQiya("0");
		} else {
			d = d / qiya.size();
			sysWeekly.setQiya(df.format(d));
		}

	}

	private String getAvgqiya(RunSiteData runSiteData) {

		double hour = 24;
		String dH0 = runSiteData.getDataH0();
		String dH1 = runSiteData.getDataH1();
		String dH2 = runSiteData.getDataH2();
		String dH3 = runSiteData.getDataH3();
		String dH4 = runSiteData.getDataH4();
		String dH5 = runSiteData.getDataH5();
		String dH6 = runSiteData.getDataH6();
		String dH7 = runSiteData.getDataH7();
		String dH8 = runSiteData.getDataH8();
		String dH9 = runSiteData.getDataH9();
		String dH10 = runSiteData.getDataH10();
		String dH11 = runSiteData.getDataH11();
		String dH12 = runSiteData.getDataH12();
		String dH13 = runSiteData.getDataH13();
		String dH14 = runSiteData.getDataH14();
		String dH15 = runSiteData.getDataH15();
		String dH16 = runSiteData.getDataH16();
		String dH17 = runSiteData.getDataH17();
		String dH18 = runSiteData.getDataH18();
		String dH19 = runSiteData.getDataH19();
		String dH20 = runSiteData.getDataH20();
		String dH21 = runSiteData.getDataH21();
		String dH22 = runSiteData.getDataH22();
		String dH23 = runSiteData.getDataH23();

		if (StringUtils.isBlank(dH0)) {
			hour--;
			dH0 = "0";
		}
		if (StringUtils.isBlank(dH1)) {
			hour--;
			dH1 = "0";
		}
		if (StringUtils.isBlank(dH2)) {
			hour--;
			dH2 = "0";
		}
		if (StringUtils.isBlank(dH3)) {
			hour--;
			dH3 = "0";
		}
		if (StringUtils.isBlank(dH4)) {
			hour--;
			dH4 = "0";
		}
		if (StringUtils.isBlank(dH5)) {
			hour--;
			dH5 = "0";
		}
		if (StringUtils.isBlank(dH6)) {
			hour--;
			dH6 = "0";
		}
		if (StringUtils.isBlank(dH7)) {
			hour--;
			dH7 = "0";
		}
		if (StringUtils.isBlank(dH8)) {
			hour--;
			dH8 = "0";
		}
		if (StringUtils.isBlank(dH9)) {
			hour--;
			dH9 = "0";
		}
		if (StringUtils.isBlank(dH10)) {
			hour--;
			dH10 = "0";
		}
		if (StringUtils.isBlank(dH11)) {
			hour--;
			dH11 = "0";
		}
		if (StringUtils.isBlank(dH12)) {
			hour--;
			dH12 = "0";
		}
		if (StringUtils.isBlank(dH13)) {
			hour--;
			dH13 = "0";
		}
		if (StringUtils.isBlank(dH14)) {
			hour--;
			dH14 = "0";
		}
		if (StringUtils.isBlank(dH15)) {
			hour--;
			dH15 = "0";
		}
		if (StringUtils.isBlank(dH16)) {
			hour--;
			dH16 = "0";
		}
		if (StringUtils.isBlank(dH17)) {
			hour--;
			dH17 = "0";
		}
		if (StringUtils.isBlank(dH18)) {
			hour--;
			dH18 = "0";
		}
		if (StringUtils.isBlank(dH19)) {
			hour--;
			dH19 = "0";
		}
		if (StringUtils.isBlank(dH20)) {
			hour--;
			dH20 = "0";
		}
		if (StringUtils.isBlank(dH21)) {
			hour--;
			dH21 = "0";
		}
		if (StringUtils.isBlank(dH22)) {
			hour--;
			dH22 = "0";
		}
		if (StringUtils.isBlank(dH23)) {
			hour--;
			dH23 = "0";
		}
		double d = 0;
		if (hour != 0) {
			d = ((Double.parseDouble(dH0) + Double.parseDouble(dH1) + Double.parseDouble(dH2) + Double.parseDouble(dH3)
					+ Double.parseDouble(dH4) + Double.parseDouble(dH5) + Double.parseDouble(dH6)
					+ Double.parseDouble(dH7) + Double.parseDouble(dH8) + Double.parseDouble(dH9)
					+ Double.parseDouble(dH10) + Double.parseDouble(dH11) + Double.parseDouble(dH12)
					+ Double.parseDouble(dH13) + Double.parseDouble(dH14) + Double.parseDouble(dH15)
					+ Double.parseDouble(dH16) + Double.parseDouble(dH17) + Double.parseDouble(dH18)
					+ Double.parseDouble(dH19) + Double.parseDouble(dH20) + Double.parseDouble(dH21)
					+ Double.parseDouble(dH22) + Double.parseDouble(dH23)) / hour);
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String format = df.format(d);
		return format;

	}

	/**
	 * 降雨
	 * 
	 * @param runSiteData
	 * @return
	 */
	private String getAvgPcp(RunSiteData runSiteData) {
		double hour = 24;
		String dH0 = runSiteData.getDataH0();
		String dH1 = runSiteData.getDataH1();
		String dH2 = runSiteData.getDataH2();
		String dH3 = runSiteData.getDataH3();
		String dH4 = runSiteData.getDataH4();
		String dH5 = runSiteData.getDataH5();
		String dH6 = runSiteData.getDataH6();
		String dH7 = runSiteData.getDataH7();
		String dH8 = runSiteData.getDataH8();
		String dH9 = runSiteData.getDataH9();
		String dH10 = runSiteData.getDataH10();
		String dH11 = runSiteData.getDataH11();
		String dH12 = runSiteData.getDataH12();
		String dH13 = runSiteData.getDataH13();
		String dH14 = runSiteData.getDataH14();
		String dH15 = runSiteData.getDataH15();
		String dH16 = runSiteData.getDataH16();
		String dH17 = runSiteData.getDataH17();
		String dH18 = runSiteData.getDataH18();
		String dH19 = runSiteData.getDataH19();
		String dH20 = runSiteData.getDataH20();
		String dH21 = runSiteData.getDataH21();
		String dH22 = runSiteData.getDataH22();
		String dH23 = runSiteData.getDataH23();

		if (StringUtils.isBlank(dH0) || dH0.equals("0.0")) {
			hour--;
			dH0 = "0";
		}
		if (StringUtils.isBlank(dH1) || dH1.equals("0.0")) {
			hour--;
			dH1 = "0";
		}
		if (StringUtils.isBlank(dH2) || dH2.equals("0.0")) {
			hour--;
			dH2 = "0";
		}
		if (StringUtils.isBlank(dH3) || dH3.equals("0.0")) {
			hour--;
			dH3 = "0";
		}
		if (StringUtils.isBlank(dH4) || dH4.equals("0.0")) {
			hour--;
			dH4 = "0";
		}
		if (StringUtils.isBlank(dH5) || dH5.equals("0.0")) {
			hour--;
			dH5 = "0";
		}
		if (StringUtils.isBlank(dH6) || dH6.equals("0.0")) {
			hour--;
			dH6 = "0";
		}
		if (StringUtils.isBlank(dH7) || dH7.equals("0.0")) {
			hour--;
			dH7 = "0";
		}
		if (StringUtils.isBlank(dH8) || dH8.equals("0.0")) {
			hour--;
			dH8 = "0";
		}
		if (StringUtils.isBlank(dH9) || dH9.equals("0.0")) {
			hour--;
			dH9 = "0";
		}
		if (StringUtils.isBlank(dH10) || dH10.equals("0.0")) {
			hour--;
			dH10 = "0";
		}
		if (StringUtils.isBlank(dH11) || dH11.equals("0.0")) {
			hour--;
			dH11 = "0";
		}
		if (StringUtils.isBlank(dH12) || dH12.equals("0.0")) {
			hour--;
			dH12 = "0";
		}
		if (StringUtils.isBlank(dH13) || dH13.equals("0.0")) {
			hour--;
			dH13 = "0";
		}
		if (StringUtils.isBlank(dH14) || dH14.equals("0.0")) {
			hour--;
			dH14 = "0";
		}
		if (StringUtils.isBlank(dH15) || dH15.equals("0.0")) {
			hour--;
			dH15 = "0";
		}
		if (StringUtils.isBlank(dH16) || dH16.equals("0.0")) {
			hour--;
			dH16 = "0";
		}
		if (StringUtils.isBlank(dH17) || dH17.equals("0.0")) {
			hour--;
			dH17 = "0";
		}
		if (StringUtils.isBlank(dH18) || dH18.equals("0.0")) {
			hour--;
			dH18 = "0";
		}
		if (StringUtils.isBlank(dH19) || dH19.equals("0.0")) {
			hour--;
			dH19 = "0";
		}
		if (StringUtils.isBlank(dH20) || dH20.equals("0.0")) {
			hour--;
			dH20 = "0";
		}
		if (StringUtils.isBlank(dH21) || dH21.equals("0.0")) {
			hour--;
			dH21 = "0";
		}
		if (StringUtils.isBlank(dH22) || dH22.equals("0.0")) {
			hour--;
			dH22 = "0";
		}
		if (StringUtils.isBlank(dH23) || dH23.equals("0.0")) {
			hour--;
			dH23 = "0";
		}
		double d = 0;
		if (hour != 0) {
			d = (Double.parseDouble(dH0) + Double.parseDouble(dH1) + Double.parseDouble(dH2) + Double.parseDouble(dH3)
					+ Double.parseDouble(dH4) + Double.parseDouble(dH5) + Double.parseDouble(dH6)
					+ Double.parseDouble(dH7) + Double.parseDouble(dH8) + Double.parseDouble(dH9)
					+ Double.parseDouble(dH10) + Double.parseDouble(dH11) + Double.parseDouble(dH12)
					+ Double.parseDouble(dH13) + Double.parseDouble(dH14) + Double.parseDouble(dH15)
					+ Double.parseDouble(dH16) + Double.parseDouble(dH17) + Double.parseDouble(dH18)
					+ Double.parseDouble(dH19) + Double.parseDouble(dH20) + Double.parseDouble(dH21)
					+ Double.parseDouble(dH22) + Double.parseDouble(dH23)) / hour;
		}

		DecimalFormat df = new DecimalFormat("0.00");
		String format = df.format(d);
		return format;

	}

	/**
	 * 天平均湿度
	 * 
	 * @param runSiteData
	 * @return
	 */
	private String getAvgHmd(RunSiteData runSiteData) {
		double hour = 24;
		String dH0 = runSiteData.getDataH0();
		String dH1 = runSiteData.getDataH1();
		String dH2 = runSiteData.getDataH2();
		String dH3 = runSiteData.getDataH3();
		String dH4 = runSiteData.getDataH4();
		String dH5 = runSiteData.getDataH5();
		String dH6 = runSiteData.getDataH6();
		String dH7 = runSiteData.getDataH7();
		String dH8 = runSiteData.getDataH8();
		String dH9 = runSiteData.getDataH9();
		String dH10 = runSiteData.getDataH10();
		String dH11 = runSiteData.getDataH11();
		String dH12 = runSiteData.getDataH12();
		String dH13 = runSiteData.getDataH13();
		String dH14 = runSiteData.getDataH14();
		String dH15 = runSiteData.getDataH15();
		String dH16 = runSiteData.getDataH16();
		String dH17 = runSiteData.getDataH17();
		String dH18 = runSiteData.getDataH18();
		String dH19 = runSiteData.getDataH19();
		String dH20 = runSiteData.getDataH20();
		String dH21 = runSiteData.getDataH21();
		String dH22 = runSiteData.getDataH22();
		String dH23 = runSiteData.getDataH23();

		if (StringUtils.isBlank(dH0)) {
			hour--;
			dH0 = "0";
		}
		if (StringUtils.isBlank(dH1)) {
			hour--;
			dH1 = "0";
		}
		if (StringUtils.isBlank(dH2)) {
			hour--;
			dH2 = "0";
		}
		if (StringUtils.isBlank(dH3)) {
			hour--;
			dH3 = "0";
		}
		if (StringUtils.isBlank(dH4)) {
			hour--;
			dH4 = "0";
		}
		if (StringUtils.isBlank(dH5)) {
			hour--;
			dH5 = "0";
		}
		if (StringUtils.isBlank(dH6)) {
			hour--;
			dH6 = "0";
		}
		if (StringUtils.isBlank(dH7)) {
			hour--;
			dH7 = "0";
		}
		if (StringUtils.isBlank(dH8)) {
			hour--;
			dH8 = "0";
		}
		if (StringUtils.isBlank(dH9)) {
			hour--;
			dH9 = "0";
		}
		if (StringUtils.isBlank(dH10)) {
			hour--;
			dH10 = "0";
		}
		if (StringUtils.isBlank(dH11)) {
			hour--;
			dH11 = "0";
		}
		if (StringUtils.isBlank(dH12)) {
			hour--;
			dH12 = "0";
		}
		if (StringUtils.isBlank(dH13)) {
			hour--;
			dH13 = "0";
		}
		if (StringUtils.isBlank(dH14)) {
			hour--;
			dH14 = "0";
		}
		if (StringUtils.isBlank(dH15)) {
			hour--;
			dH15 = "0";
		}
		if (StringUtils.isBlank(dH16)) {
			hour--;
			dH16 = "0";
		}
		if (StringUtils.isBlank(dH17)) {
			hour--;
			dH17 = "0";
		}
		if (StringUtils.isBlank(dH18)) {
			hour--;
			dH18 = "0";
		}
		if (StringUtils.isBlank(dH19)) {
			hour--;
			dH19 = "0";
		}
		if (StringUtils.isBlank(dH20)) {
			hour--;
			dH20 = "0";
		}
		if (StringUtils.isBlank(dH21)) {
			hour--;
			dH21 = "0";
		}
		if (StringUtils.isBlank(dH22)) {
			hour--;
			dH22 = "0";
		}
		if (StringUtils.isBlank(dH23)) {
			hour--;
			dH23 = "0";
		}
		double d = 0;
		if (hour != 0) {
			d = ((Double.parseDouble(dH0) + Double.parseDouble(dH1) + Double.parseDouble(dH2) + Double.parseDouble(dH3)
					+ Double.parseDouble(dH4) + Double.parseDouble(dH5) + Double.parseDouble(dH6)
					+ Double.parseDouble(dH7) + Double.parseDouble(dH8) + Double.parseDouble(dH9)
					+ Double.parseDouble(dH10) + Double.parseDouble(dH11) + Double.parseDouble(dH12)
					+ Double.parseDouble(dH13) + Double.parseDouble(dH14) + Double.parseDouble(dH15)
					+ Double.parseDouble(dH16) + Double.parseDouble(dH17) + Double.parseDouble(dH18)
					+ Double.parseDouble(dH19) + Double.parseDouble(dH20) + Double.parseDouble(dH21)
					+ Double.parseDouble(dH22) + Double.parseDouble(dH23)) / hour) / 100;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String format = df.format(d);
		return format;

	}

	/**
	 * 平均风速/天
	 * 
	 * @param runSiteData
	 * @return
	 */
	private String getAvgWnd(RunSiteData runSiteData) {

		double hour = 24;
		String dH0 = runSiteData.getDataH0();
		String dH1 = runSiteData.getDataH1();
		String dH2 = runSiteData.getDataH2();
		String dH3 = runSiteData.getDataH3();
		String dH4 = runSiteData.getDataH4();
		String dH5 = runSiteData.getDataH5();
		String dH6 = runSiteData.getDataH6();
		String dH7 = runSiteData.getDataH7();
		String dH8 = runSiteData.getDataH8();
		String dH9 = runSiteData.getDataH9();
		String dH10 = runSiteData.getDataH10();
		String dH11 = runSiteData.getDataH11();
		String dH12 = runSiteData.getDataH12();
		String dH13 = runSiteData.getDataH13();
		String dH14 = runSiteData.getDataH14();
		String dH15 = runSiteData.getDataH15();
		String dH16 = runSiteData.getDataH16();
		String dH17 = runSiteData.getDataH17();
		String dH18 = runSiteData.getDataH18();
		String dH19 = runSiteData.getDataH19();
		String dH20 = runSiteData.getDataH20();
		String dH21 = runSiteData.getDataH21();
		String dH22 = runSiteData.getDataH22();
		String dH23 = runSiteData.getDataH23();

		if (StringUtils.isBlank(dH0)) {
			hour--;
			dH0 = "00.0";
		}
		if (StringUtils.isBlank(dH1)) {
			hour--;
			dH1 = "00.0";
		}
		if (StringUtils.isBlank(dH2)) {
			hour--;
			dH2 = "00.0";
		}
		if (StringUtils.isBlank(dH3)) {
			hour--;
			dH3 = "00.0";
		}
		if (StringUtils.isBlank(dH4)) {
			hour--;
			dH4 = "00.0";
		}
		if (StringUtils.isBlank(dH5)) {
			hour--;
			dH5 = "00.0";
		}
		if (StringUtils.isBlank(dH6)) {
			hour--;
			dH6 = "00.0";
		}
		if (StringUtils.isBlank(dH7)) {
			hour--;
			dH7 = "00.0";
		}
		if (StringUtils.isBlank(dH8)) {
			hour--;
			dH8 = "00.0";
		}
		if (StringUtils.isBlank(dH9)) {
			hour--;
			dH9 = "00.0";
		}
		if (StringUtils.isBlank(dH10)) {
			hour--;
			dH10 = "00.0";
		}
		if (StringUtils.isBlank(dH11)) {
			hour--;
			dH11 = "00.0";
		}
		if (StringUtils.isBlank(dH12)) {
			hour--;
			dH12 = "00.0";
		}
		if (StringUtils.isBlank(dH13)) {
			hour--;
			dH13 = "00.0";
		}
		if (StringUtils.isBlank(dH14)) {
			hour--;
			dH14 = "00.0";
		}
		if (StringUtils.isBlank(dH15)) {
			hour--;
			dH15 = "00.0";
		}
		if (StringUtils.isBlank(dH16)) {
			hour--;
			dH16 = "00.0";
		}
		if (StringUtils.isBlank(dH17)) {
			hour--;
			dH17 = "00.0";
		}
		if (StringUtils.isBlank(dH18)) {
			hour--;
			dH18 = "00.0";
		}
		if (StringUtils.isBlank(dH19)) {
			hour--;
			dH19 = "00.0";
		}
		if (StringUtils.isBlank(dH20)) {
			hour--;
			dH20 = "00.0";
		}
		if (StringUtils.isBlank(dH21)) {
			hour--;
			dH21 = "00.0";
		}
		if (StringUtils.isBlank(dH22)) {
			hour--;
			dH22 = "00.0";
		}
		if (StringUtils.isBlank(dH23)) {
			hour--;
			dH23 = "00.0";
		}

		String s0 = dH0.substring(1, 4);
		String s1 = dH1.substring(1, 4);
		String s2 = dH2.substring(1, 4);
		String s3 = dH3.substring(1, 4);
		String s4 = dH4.substring(1, 4);
		String s5 = dH5.substring(1, 4);
		String s6 = dH6.substring(1, 4);
		String s7 = dH7.substring(1, 4);
		String s8 = dH8.substring(1, 4);
		String s9 = dH9.substring(1, 4);
		String s10 = dH10.substring(1, 4);
		String s11 = dH11.substring(1, 4);
		String s12 = dH12.substring(1, 4);
		String s13 = dH13.substring(1, 4);
		String s14 = dH14.substring(1, 4);
		String s15 = dH15.substring(1, 4);
		String s16 = dH16.substring(1, 4);
		String s17 = dH17.substring(1, 4);
		String s18 = dH18.substring(1, 4);
		String s19 = dH19.substring(1, 4);
		String s20 = dH20.substring(1, 4);
		String s21 = dH21.substring(1, 4);
		String s22 = dH22.substring(1, 4);
		String s23 = dH23.substring(1, 4);
		double d = 0;
		if (hour != 0) {
			d = (Double.parseDouble(s0) + Double.parseDouble(s1) + Double.parseDouble(s2) + Double.parseDouble(s3)
					+ Double.parseDouble(s4) + Double.parseDouble(s5) + Double.parseDouble(s6) + Double.parseDouble(s7)
					+ Double.parseDouble(s8) + Double.parseDouble(s9) + Double.parseDouble(s10)
					+ Double.parseDouble(s11) + Double.parseDouble(s12) + Double.parseDouble(s13)
					+ Double.parseDouble(s14) + Double.parseDouble(s15) + Double.parseDouble(s16)
					+ Double.parseDouble(s17) + Double.parseDouble(s18) + Double.parseDouble(s19)
					+ Double.parseDouble(s20) + Double.parseDouble(s21) + Double.parseDouble(s22)
					+ Double.parseDouble(s23)) / hour;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String format = df.format(d);
		return format;
	}
}
